home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / coding / cpp / temp.cpp < prev    next >
C/C++ Source or Header  |  1995-03-23  |  5KB  |  183 lines

  1. //    Programmer:        Ralph Looney
  2. //  PH        :   (619) 744 - 4000 x498 or x497
  3. //  FAX       :   (619) 471 - 6255
  4. //
  5. // This program is based on the example in
  6. // Stanley B. Lippman C++ Primer 2nd edition book
  7. // Page 50 - 55
  8. // Page 146 - 148
  9. // Page 380 - 386
  10. //
  11. // As it is now, the program compiles/run BUT does not use the
  12. // overloaded cout << operator ???
  13.  
  14.  
  15.  
  16. #include <iostream.h>
  17. #include <assert.h>
  18. #include <string.h>
  19. #include <conio.h>
  20.  
  21. const int ArraySize = 12;                        // default size
  22.  
  23. /*******************TEMPLATE*********************/
  24. /* IntArray Class Definition                    */
  25. /* Tag Name = IntArray                          */
  26. /* Member Function/Interface                    */
  27. /*         - getsize                                                                    */
  28. /*        - grow                                                                        */
  29. /*        - operator[]    (overloading [] )                      */
  30. /*        - print                (overloading << )                        */
  31. /* Member Variable                              */
  32. /*        - size                                                          */
  33. /*        - pointer ia                                                            */
  34. /************************************************/
  35. template <class MyType>
  36. class IntArray
  37. {
  38. public:
  39.     IntArray( int sz = ArraySize);                                 // constructors
  40.  ~IntArray() {delete[] ia;}                      // destructor
  41.     MyType getsize() { return size; }
  42.     MyType& operator[] (int);
  43.     void grow();
  44.  
  45.     void print(ostream& = cout);                   // overloading <<
  46.  
  47. protected:                                       // accessible from
  48.     int size;                                      // inside class
  49.     MyType *ia;                                    // only
  50. };
  51.  
  52.  
  53. /*****************CONSTRUCTOR********************/
  54. template<class MyType> IntArray<MyType>::IntArray( int sz)
  55. {
  56.     size = sz;
  57.     ia = new MyType[size];                         // allocating memory
  58.     assert( ia != 0);
  59.     for (int ix=0; ix < sz; ++ix)
  60.              ia[ix] = 0;
  61. }
  62.  
  63. /*********************GROW***********************/
  64. template<class MyType> void IntArray<MyType>::grow()
  65. {
  66.     int oldsize = size;
  67.     MyType *oldia = ia;
  68.  
  69.     size += size/2 + 1;                            // increase by 1/2 + 1
  70.     ia = new MyType[size];                         // allocating memory
  71.  
  72.     int ix = 0;
  73.     for ( ; ix < oldsize; ++ix)                    // copying old array
  74.             ia[ix] =  oldia[ix];                       // into new array
  75.  
  76.     for ( ; ix < size; ++ix) ia[ix] = 0;           // initializing new part
  77.     delete oldia;                                  // deallocate memory
  78. }
  79. /******************OPERATOR[]********************/
  80. template<class MyType> inline MyType&
  81. IntArray<MyType>::operator[] (int index)
  82. {
  83.      assert(index >=0 && index < size);            // checks bounds
  84.      return ia[ index ];
  85. }
  86.  
  87. // --------------------------------------------------------
  88. // --------------------------------------------------------
  89. template <class MyType> ostream&
  90. operator<<( ostream_withassign& os, IntArray<MyType>& ar)
  91. {
  92.     ar.print(os);
  93.     return os;
  94. }
  95. // =====
  96. template <class MyType> void IntArray<MyType>::print(ostream& os)
  97. {
  98.     const lineLength = 12;
  99.  
  100.   cout << "got here" << endl;
  101.     os << "( " << size << " )< ";
  102.  
  103.     for (int ix = 0; ix < size; ++ix)
  104.     {
  105.         if (ix % lineLength == 0 && ix) os << "\n\t";
  106.         os << ia[ ix ];
  107.  
  108.         if (ix % lineLength != lineLength-1 &&
  109.                          ix != size-1) os << ", ";
  110.     }
  111.     os << " >\n";
  112. }
  113. /************************************************/
  114. /*                  M A I N                     */
  115. /************************************************/
  116. int main()
  117. {
  118.     clrscr();
  119.     int ix = 0;
  120.     char Response;
  121.  
  122.     IntArray <int> ia;                             // Integer    instantiation
  123.     IntArray <double> da(10);                      // Double     instantiation
  124.     IntArray <char> sa(3);                         // Character  instantiation
  125.  
  126.     cout << "DISPLAY SIZE:" << endl;
  127. //         -------------
  128.     cout << "size: " << ia.getsize() << endl;
  129.     cout << "size: " << da.getsize() << endl;
  130.     cout << "size: " << sa.getsize() << endl;
  131.  
  132.     for (ix=0; ix < ia.getsize(); ++ix )
  133.             ia[ix] = ix * 2;                           // initialize 0,2,4, ..
  134.     for (ix=0; ix < da.getsize(); ++ix )
  135.             da[ix] = ix * 3;                           // initialize 0,3,6, ..
  136.     for (ix=0; ix < sa.getsize(); ++ix )
  137.             sa[ix] = ix + 65;                          // initialize A,B,C, ..
  138.  
  139.     cout << endl;
  140.     cout << ia << endl;
  141. /*
  142.     cout << "DISPLAY ARRAY:" << endl;
  143. //         --------------
  144.     for ( ix=0; ix < ia.getsize(); ++ix )
  145.             cout << ia[ix] << " ";
  146.             cout << endl;
  147.     for ( ix=0; ix < da.getsize(); ++ix )
  148.             cout << da[ix] << " ";
  149.             cout << endl;
  150.     for ( ix=0; ix < sa.getsize(); ++ix )
  151.             cout << sa[ix] << " ";
  152.             cout << endl;
  153.  
  154.     cout << endl;
  155.     cout << "GROWING ARRAY'S:" << endl;
  156. //         ----------------
  157.     ia.grow();
  158.     da.grow();
  159.     sa.grow();
  160.  
  161.     cout << endl;
  162.     cout << "DISPLAY GROWN ARRAY:" << endl;
  163. //         --------------------
  164.     for ( ix=0; ix < ia.getsize(); ++ix )
  165.             cout << ia[ix] << " ";
  166.             cout << endl;
  167.     for ( ix=0; ix < da.getsize(); ++ix )
  168.             cout << da[ix] << " ";
  169.             cout << endl;
  170.     for ( ix=0; ix < sa.getsize(); ++ix )
  171.             cout << sa[ix] << " ";
  172.             cout << endl;
  173.  
  174.     cout << endl;
  175.     cout << "TEST BOUNDS CHECKING:" << endl;
  176. //         ---------------------
  177.     for ( ix=0; ix < ia.getsize() + 2; ++ix )      // size PLUS 2
  178.             cout << ia[ix] << " ";                     // out of bound
  179.             cout << endl;
  180. */
  181.     return 0;
  182. }
  183.